package com.wokeMy.view;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.wokeMy.view.adpter.RecordDhAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.GhtZd;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class RecordCreditActivity extends BaseActivity {
    ImageView reccback;
    ListView recclv;
    String user_id;
    Dialog dialog;
    ArrayList<GhtZd> ghtList ;
    RecordDhAdapter recordDhAdapter;
    int way_id;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_record_credit);
    }

    @Override
    public void initView() {
        recclv = (ListView) findViewById(R.id.recc_lv);
        reccback = (ImageView) findViewById(R.id.recc_back);
        reccback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        way_id = getIntent().getIntExtra ("way_id", 0);
        ghtList= new ArrayList<>();
    }
    @Override
    public void initActivity() {
        tishi("如果您有订单交易失败，可以点击对应的订单，系统将会为您重新发起交易");
        Log.e("RecordCreditActivity",way_id+"");
        recordDhAdapter=new RecordDhAdapter(this,ghtList,way_id);
        recclv.setAdapter(recordDhAdapter);
        ghtLog();
    }
    // 高汇通日志接口
    public void  ghtLog(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        Log.e("高汇通日志接口params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.GHTLOG_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("高汇通日志接口result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            Gson gson = new Gson();
                            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
                            for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                                GhtZd ghtzd = gson.fromJson(jsonArray.getString(i1), GhtZd.class);
                                ghtList.add(ghtzd);
                            }
                            recordDhAdapter.notifyDataSetChanged();
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(dialog);
            }
        });
    }

}
